<template>
  <div class="app-container">
    <div class="search-container">
      <!-- 查询 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">

        <el-form-item label="转种日期">
          <el-date-picker v-model="formInline.startDate" type="date" placeholder="开始日期" clearable
            style="width: 150px;" />-
          <el-date-picker v-model="formInline.endDate" type="date" placeholder="结束日期" clearable style="width: 150px;" />
        </el-form-item>

        <el-form-item label="后裔耳号">
          <el-input v-model="formInline.eatTag" placeholder="请输入羔羊耳号" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="getData()">查询</el-button>
          <el-button type="danger" @click="pDel()">批量删除</el-button>
          <el-button type="success" @click="add()">添加</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据 -->
      <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="descendantId" label="序号" />
        <el-table-column prop="descendantDate" label="转种日期" width="180px" />
        <el-table-column prop="earTag" label="育成羊耳号" />
        <el-table-column prop="toBuildingManageName" label="转入栋舍" />
        <el-table-column prop="toColumMangementName" label="转入栏位" />
        <el-table-column prop="currentBuildingName" label="当前栋舍" />
        <el-table-column prop="currentFieName" label="当前栏位" />
        <el-table-column prop="descendantSex" label="性别">
          <template v-slot="scope">
            <span v-if="scope.row.descendantSex == 1">公</span>
            <span v-else>母</span>
          </template>
        </el-table-column>
        <el-table-column label="转种日龄">
          <template v-slot="scope">
            {{ calculateDaysDifference(scope.row.descendantDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="createName" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" width="180px" />
        <el-table-column fixed="right" label="操作" width="200px">
          <template v-slot="scope">
            <el-button link type="danger" @click="dels(scope.row.earTag)">删除</el-button>
            <el-button link type="primary" @click="edit(scope.row.earTag)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="formInline.pageIndex" v-model:page-size="formInline.pageSize"
        :page-sizes="[10, 15, 20, 25]" :background="true" layout="total, sizes, prev, pager, next, jumper"
        :total="pages.totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import transfer from '@/api/ProgenyTransferment/index'
const router = useRouter()
//查询参数
const formInline = reactive({
  "startDate": "",
  "endDate": "",
  "eatTag": "",
  "pageSize": 15,
  "pageIndex": 1
})
//分页
const pages = reactive({
  totalCount: 0,
  pageCount: 0
})
//分页
const handleSizeChange = (val: number) => {
  formInline.pageSize = val
  getData()
}
const handleCurrentChange = (val: number) => {
  formInline.pageIndex = val
  getData()
}
//数据
const tableData = ref([])
//获取数据
const getData = async () => {
  transfer.getDescend(formInline).then(res => {
    tableData.value = res.data.data
    pages.totalCount = res.data.totalCount
    pages.pageCount = res.data.pageCount
  })
}
//计算天数差
const calculateDaysDifference = (descendantDate: any) =>
  Math.floor((Date.now() - new Date(descendantDate).getTime()) / (1000 * 60 * 60 * 24));
//删除
const dels = (id: any) => {
  const idss = [id]
  const ids = reactive({
    ids: idss
  })
  console.log(ids)
  ElMessageBox.confirm(
    '确认要删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    transfer.deltetes(idss).then((res: any) => {
      if (res.code > 0) {
        ElMessage.success('删除成功');
        getData();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除已取消',
    });
  });
};
//批量删除
const multipleSelection = ref([])
//批量删除
const handleSelectionChange = (val: any) => {
  val.forEach(element => {
    multipleSelection.value.push(element.descendantId)
  });
}
//批量删除
const pDel = () => {
  ElMessageBox.confirm(
    '确认要删除吗?',
    '警告',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    transfer.deltetes(multipleSelection.value).then((res: any) => {
      if (res.code > 0) {
        ElMessage.success('删除成功');
        getData();
      }
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除已取消',
    });
  });
};
//添加
const add = () => {
  router.push({ name: 'ProgenyTransferAdd' })
}
//修改
const edit = (eartag: any) => {
  router.push({
    path: '/ProgenyTransferUpd',
    query: {
      eartag: eartag
    }

  })
}
onMounted(() => {
  getData()
  calculateDaysDifference
})

</script>